home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-08-10 | 3.2 KB | 117 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CUDPServerThread.cp ©1995-1998 Metrowerks Inc. All rights reserved.
- // ===========================================================================
-
- #include "CUDPServerThread.h"
-
- #include <PP_KeyCodes.h>
-
-
- // ---------------------------------------------------------------------------
- // • CUDPServerThread
- // ---------------------------------------------------------------------------
-
- CUDPServerThread::CUDPServerThread(
- UInt16 inLocalPort,
- PP_PowerPlant::LUDPEndpoint *inNetworkEndpoint,
- CSimpleUDPServer* inServerMaster,
- CTerminalPane* inTerminalPane)
- : LThread( false,
- PP_PowerPlant::thread_DefaultStack,
- PP_PowerPlant::LThread::threadOption_Default,
- nil),
- mPort(inLocalPort),
- mEndpoint(inNetworkEndpoint),
- mServerMaster(inServerMaster),
- mTerminalPane(inTerminalPane)
- {
- mContinue = true;
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CUDPServerThread
- // ---------------------------------------------------------------------------
-
- CUDPServerThread::~CUDPServerThread()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • Complete
- // ---------------------------------------------------------------------------
-
- void
- CUDPServerThread::Complete()
- {
- if (mContinue) {
- mContinue = false;
- mEndpoint->AbortThreadOperation(this);
- }
- }
-
- // ---------------------------------------------------------------------------
- // • Run
- // ---------------------------------------------------------------------------
-
- void*
- CUDPServerThread::Run()
- {
- try {
-
- // Initialization: Bind to the port specified
- PP_PowerPlant::LInternetAddress address(0, mPort);
- mEndpoint->Bind(address, 1);
- mServerMaster->BindCompleted();
-
- // Endless loop: watch and wait for incomming UDP data.
- while (mContinue) {
- Try_ {
- PP_PowerPlant::LInternetAddress remoteAddress;
- char dataBuffer[80];
- UInt32 ioDataSize = 80;
-
- mEndpoint->ReceiveFrom(remoteAddress, dataBuffer, ioDataSize);
-
- //Get the IP Address/Port of the sender
- Str255 remoteAddressString;
- remoteAddress.GetIPDescriptor(remoteAddressString, true);
- PP_PowerPlant::LStr255 remoteAddressDisplay = remoteAddressString;
-
- //Add CRLF
- remoteAddressDisplay += PP_PowerPlant::char_Return;
- remoteAddressDisplay += PP_PowerPlant::char_LineFeed;
-
- //Display the remote address info
- mTerminalPane->DoWriteBfr((const char *)&remoteAddressDisplay[1], remoteAddressDisplay[0]);
-
- //Display the UDP data
- mTerminalPane->DoWriteBfr(dataBuffer, ioDataSize);
-
- //Turn the data back arround to the sender
- mEndpoint->SendPacketData(remoteAddress, dataBuffer, ioDataSize);
- }
- Catch_ (inErr) {
- //Examples of things to watch for
- if (inErr == PP_PowerPlant::Timeout_Error) {
- continue; // you might want to break here instead
- }
- else if (inErr == PP_PowerPlant::Abort_Error) {
- break;
- }
- else {
- break;
- }
- }
- }
- }
-
- catch(...) {
- // We're done with the connection… bail out.
- mEndpoint->Unbind();
- }
-
- mServerMaster->ServerThreadDied();
- return nil;
- }
-